package com.ruoyi.resume.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;



import com.ruoyi.resume.domain.*;
import com.ruoyi.resume.domain.vo.LoginVo;
import com.ruoyi.resume.domain.vo.TbStudentVo;
import com.ruoyi.resume.mapper.*;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.resume.service.ICvInformationService;

/**
 * 简历基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-30
 */
@Service
public class CvInformationServiceImpl implements ICvInformationService 
{
    @Autowired
    private CvInformationMapper cvInformationMapper;
    @Autowired
    private CvEducationalBackgroundMapper cvEducationalBackgroundMapper;

    @Autowired
    private CvProjectExperienceMapper cvProjectExperienceMapper;

    @Autowired
    private CvSkillMapper cvSkillMapper;

    @Autowired
    private CvTechnologyMapper cvTechnologyMapper;

    @Autowired
    private CvWorkExperienceMapper cvWorkExperienceMapper;

    @Autowired

    private BwBackgroundMapper bwBackgroundMapper;




    /**
     * 查询简历基本信息
     * 
     * @param id 简历基本信息主键
     * @return 简历基本信息
     */
    @Override
    public CvInformation selectCvInformationById(Long id)
    {
        return cvInformationMapper.selectCvInformationById(id);
    }

    /**
     * 查询简历基本信息列表
     * 
     * @param cvInformation 简历基本信息
     * @return 简历基本信息
     */
    @Override
    public List<CvInformation> selectCvInformationList(CvInformation cvInformation)
    {
        return cvInformationMapper.selectCvInformationList(cvInformation);
    }

    /**
     * 新增简历基本信息
     * 
     * @param cvInformation 简历基本信息
     * @return 结果
     */
    @Override
    public int insertCvInformation(CvInformation cvInformation)
    {
        cvInformation.setCreateTime(DateUtils.getNowDate());

        //查询是否存在手机号
//        CvInformation cvInformation1 =  cvInformationMapper.selectCvInformationByMobile(cvInformation.getStudentMobile());
//        if (cvInformation1 != null){
//            //存在手机号
//            return 3;
//        }
        Long studentId = cvInformation.getStudentId();
        TbStudentVo tbStudentVo = cvInformationMapper.selectStudentInfo(studentId);
        if (tbStudentVo == null){
            return -1;
        }
        String stuMobile = tbStudentVo.getStuMobile();
        if (!stuMobile.equals(cvInformation.getStudentMobile())){
            cvInformationMapper.updateStudentInfo(tbStudentVo.getId(),cvInformation.getStudentMobile());
        }



        int code = 0;

        code= cvInformationMapper.insertCvInformation(cvInformation);
        //添加教育背景表
        for (CvEducationalBackground cvEducationalBackground : cvInformation.getCvEducationalBackgrounds()) {
            if (cvEducationalBackground!=null){
                cvEducationalBackground.setInformationId(cvInformation.getId());
                code = cvEducationalBackgroundMapper.insertCvEducationalBackground(cvEducationalBackground);
            }
        }


        //添加专项技能表
        for (CvSkill cvSkill : cvInformation.getCvSkills()) {
            if (cvSkill != null){
                String skillMessage = cvSkill.getSkillBefore()+cvSkill.getSkillAfter();
                cvSkill.setSkillMessage(skillMessage);
                cvSkill.setInformationId(cvInformation.getId());
                cvSkill.setCreatedTime(new Date());
                code = cvSkillMapper.insertCvSkill(cvSkill);
            }
        }

        //添加工作经历表
        for (CvWorkExperience cvWorkExperience : cvInformation.getCvWorkExperiences()) {
            if (cvWorkExperience != null){
                cvWorkExperience.setInformationId(cvInformation.getId());
                cvWorkExperience.setCreatedTime(new Date());
                code = cvWorkExperienceMapper.insertCvWorkExperience(cvWorkExperience);
            }
        }

        return code;
    }

    /**
     * 修改简历基本信息
     * 
     * @param cvInformation 简历基本信息
     * @return 结果
     */
    @Override
    public int updateCvInformation(CvInformation cvInformation)
    {
        cvInformation.setUpdateTime(DateUtils.getNowDate());

        Long studentId = cvInformation.getStudentId();
        TbStudentVo tbStudentVo = cvInformationMapper.selectStudentInfo(studentId);
        if (tbStudentVo == null){
            return -1;
        }
        String stuMobile = tbStudentVo.getStuMobile();
        if (!stuMobile.equals(cvInformation.getStudentMobile())){
            cvInformationMapper.updateStudentInfo(tbStudentVo.getId(),cvInformation.getStudentMobile());

        }

        int code = 0;

        code= cvInformationMapper.updateCvInformation(cvInformation);
        //删除教育背景表
        code = cvEducationalBackgroundMapper.deleteCvEducationalBackgroundByInformationId(cvInformation.getId());
        //添加教育背景表
        for (CvEducationalBackground cvEducationalBackground : cvInformation.getCvEducationalBackgrounds()) {
            if (cvEducationalBackground!=null){
                cvEducationalBackground.setInformationId(cvInformation.getId());
                code = cvEducationalBackgroundMapper.insertCvEducationalBackground(cvEducationalBackground);
            }
        }

       cvSkillMapper.deleteCvSkillByInformationId(cvInformation.getId());
        //添加专项技能表
        for (CvSkill cvSkill : cvInformation.getCvSkills()) {
            if (cvSkill != null){
                String skillMessage = cvSkill.getSkillBefore()+cvSkill.getSkillAfter();
                cvSkill.setSkillMessage(skillMessage);
                cvSkill.setInformationId(cvInformation.getId());
                cvSkill.setCreatedTime(new Date());
                code = cvSkillMapper.insertCvSkill(cvSkill);
            }
        }
        //删除经历表
        code = cvWorkExperienceMapper.deleteCvWorkExperienceByInformationId(cvInformation.getId());
        //添加工作经历表
        for (CvWorkExperience cvWorkExperience : cvInformation.getCvWorkExperiences()) {
            if (cvWorkExperience != null){
                cvWorkExperience.setInformationId(cvInformation.getId());
                cvWorkExperience.setCreatedTime(new Date());
                code = cvWorkExperienceMapper.insertCvWorkExperience(cvWorkExperience);
            }
        }




        return code;
    }

    /**
     * 批量删除简历基本信息
     * 
     * @param ids 需要删除的简历基本信息主键
     * @return 结果
     */
    @Override
    public int deleteCvInformationByIds(Long[] ids)
    {
        for (Long id : ids) {
            //删除教育背景表
            cvEducationalBackgroundMapper.deleteCvEducationalBackgroundByInformationId(id);
            //删除经历表
            cvWorkExperienceMapper.deleteCvWorkExperienceByInformationId(id);
//            cvSkillMapper.deleteCvSkillByInformationId(id);

        }
        return cvInformationMapper.deleteCvInformationByIds(ids);
    }

    /**
     * 删除简历基本信息信息
     * 
     * @param id 简历基本信息主键
     * @return 结果
     */
    @Override
    public int deleteCvInformationById(Long id)
    {
        int code = 0;
        //删除教育背景表
        code = cvEducationalBackgroundMapper.deleteCvEducationalBackgroundByInformationId(id);
        //删除经历表
        code = cvWorkExperienceMapper.deleteCvWorkExperienceByInformationId(id);
        //删除简历基本信息信息
        code = cvInformationMapper.deleteCvInformationById(id);
        return code;
    }



    @Override
    public TbStudentVo interviewLogin(LoginVo loginVo) {
        TbStudentVo tbStudentVo =  cvInformationMapper.interviewLogin(loginVo);

        return tbStudentVo ;
    }

    @Override
    public R informMobile(String studentMobile) {
        CvInformation cvInformation = cvInformationMapper.selectCvInformationByMobile(studentMobile);

        return R.ok(cvInformation);
    }

    @Override
    public R backgroundList() {
        List<BwBackground> backgrounds = bwBackgroundMapper.selectBackground();
        return R.ok(backgrounds);
    }


}
